package controller;

import com.zuche.framework.controller.CommonController;
import com.zuche.framework.tradingPlatform.*;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;



/** 
 * @Description:  
 * @author 张俊亮（jl.zhang07@zuche.com）2015-5-29 上午10:20:13
 * @version  
 */
@Controller
@RequestMapping("/test")
public class TradingTestController extends CommonController {
	@RequestMapping("/testMvcMode")
	public ModelAndView testMvcMode(HttpServletRequest request,HttpServletResponse response){
		System.out.println("entering controller");
		Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put(RequestParams.SOURCE_ID, CurrenceSource.JDPAY_M);//CurrenceSource.KUAIQIAN,"jdpay_sdk"JDPAY_M,"CCB"
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.67:8080/cartptest/testCartp/testRpcModeReturn.do_");
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.WEB_BANK);//GroupType.WEB_BANK,"jdSDK"
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_MONEY, "0.01");
        paramMap.put(RequestParams.BUSINESS_NAME, "testOrder");//kuaiqian
        paramMap.put(RequestParams.PAYER_ID, "5125100");//kuaiqian
//        paramMap.put(RequestParams.CUSTOMIZED_FLAG + "sysFlag", "Iphone");
//        paramMap.put(RequestParams.BUSINESS_NAME, "交易名称");
//        paramMap.put(RequestParams.PAYER_ID, "zhjl");
        paramMap.put(RequestParams.SYS_GROUP, "1");//1专车0租车
        paramMap.put(RequestParams.PAYER_TEL,"15902283216");
//        paramMap.put("tp_jdtoken","822116ba3ddcece66a66973b0c48c0f6");
		TradingServiceExecutor.doMvc(request, response, paramMap, true);
		return null;
	}

	@RequestMapping("/testMvcModeReturn")
	@ResponseBody
	public String testMvcModeReturn(HttpServletRequest request,HttpServletResponse response){
		System.out.println("MVC 回调开始");
        String status = request.getParameter(ReplyRequestParams.STATUS);
        String recordId = request.getParameter(ReplyRequestParams.RECORD_ID);
        String replyId = request.getParameter(ReplyRequestParams.REPLY_ID);
        String jdToken=request.getParameter("reply_tp_jd_token");
//        String payMoney=request.getParameter("reply_tp_pay_money");
        System.out.println(jdToken);
        System.out.println("status is "+ status);
        System.out.println("record is "+ recordId);
        System.out.println("replyId is "+ replyId);
        if (Status.SUCCESS.equals(status)) {
            System.out.println("开始处理回调");
		}
		System.out.println("MVC 回调结束");
		
		response.addHeader("CAR_TP" , Boolean.TRUE.toString());
		return "http://www.10101111.com/";
		
	}


    @RequestMapping("/testRpcMode.do_")
    @ResponseBody
    public void testRpcMode(HttpServletRequest request,HttpServletResponse response){
        Map<String,String> paramMap = new HashMap<String,String>();
        response.setContentType("text/plain;charset=utf-8");
        PrintWriter out = null;
        try {
            out=response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*paramMap.put(RequestParams.GROUP_TYPE, GroupType.EPOS_PAY);//GroupType.EPOS_PAY
//        paramMap.put(RequestParams.PAYER_ID,"3008404");
//        paramMap.put(RequestParams.CREDIT_ID, "1234");
        paramMap.put(RequestParams.PAYER_ID,"1600513");
        paramMap.put(RequestParams.CREDIT_ID, "19");
        paramMap.put(RequestParams.BUSINESS_ID, "test666");
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.BUSINESS_NAME, "testOrder");
        paramMap.put(RequestParams.CUSTOMIZED_FLAG + "selfParam", "liqingyuan");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,
                "http://10.101.32.104:8080/cartptest/testCartp/testRpcModeReturn.do_");
//        定义自定义参数
        paramMap.put(RequestParams.CUSTOMIZED_FLAG +"selfParam", "liqingyuan");
        paramMap.put(RequestParams.SYS_GROUP, "1");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        //提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/
        /*
        paramMap.put(RequestParams.SOURCE_ID,"jdpay_sdk");//CurrenceSource.KUAIQIAN,"jdpay_sdk"
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        paramMap.put(RequestParams.GROUP_TYPE, "jdSDK");//GroupType.WEB_BANK,"jdSDK"
        paramMap.put(RequestParams.BUSINESS_ID, ""+System.currentTimeMillis());
        paramMap.put(RequestParams.BUSINESS_MONEY, "0.11");
        paramMap.put(RequestParams.BUSINESS_NAME, "交易名称");
        paramMap.put(RequestParams.PAYER_ID, "zhjl");
        paramMap.put(RequestParams.SYS_GROUP, "1");
        paramMap.put(RequestParams.PAYER_TEL,"15902283216");
        paramMap.put("tp_jdtoken","822116ba3ddcece66a66973b0c48c0f6");
        //工具类可以自行指定是否为测试请求，也可使用doRpcBySetting方法根据配置自动选择
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);*/



        //撤销预授权
        /*paramMap.put(RequestParams.SYS_GROUP, "1");
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.EPOS_AUTH_CANCEL);
        String[] recordId = { "11127230416724204"};
        for (int i =0;i< recordId.length;i++){
        paramMap.put(RequestParams.RECORD_ID,recordId[i]);//1125745025033244,0125726636373447
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, false);
//        提取同步回复信息
        System.out.println("recordId is "+recordId[i]+":"+result.get(ReplyRequestParams.STATUS));
         out.print(JSONObject.fromObject(result));
        }*/
        /*paramMap.put(RequestParams.RECORD_ID,"1125773060362164");//1125745025033244,0125726636373447
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, false);
//        提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/
        //文件
        /*try {
            paramMap.put(RequestParams.GROUP_TYPE, GroupType.EPOS_AUTH_CANCEL);
            paramMap.put(RequestParams.SYS_GROUP,"1");
            paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
            FileReader file = new FileReader("E:\\work\\ucar\\s.txt");
            BufferedReader reader = new BufferedReader(file);
            String input;
            List<String> list = new ArrayList<String>();
            int i =1;
            while((input=reader.readLine())!=null){
                paramMap.put(RequestParams.RECORD_ID, input);
                Map<String,String> result=TradingServiceExecutor.doRpc(paramMap,false);
                System.out.println("第i个的结果是i=："+  i++  +"%%" +input+"&&&&&&&&&&&&&&&&&&&====="+result.get(ReplyRequestParams.STATUS));
                out.print(JSONArray.fromObject(result));
                if (result.get(ReplyRequestParams.STATUS).equals("0")) {
                    list.add(input);
                }
            }
            System.out.println(list);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }*/

        //预授权完成撤销
        /*paramMap.put(RequestParams.RECORD_ID,"01027231262167235");//1125745025033244,0125726636373447
        paramMap.put(RequestParams.SYS_GROUP, "1");
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.EPOS_AUTH_CONF_CANCEL);
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, false);
//        提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/

        //快捷通单笔退款 heige 0126002061704274 ce2
        /*paramMap.put(RequestParams.GROUP_TYPE, GroupType.EPOS_PAY_REFUND);//先取消后退款GroupType.EPOS_PAY_CANCEL,EPOS_PAY_REFUND
        paramMap.put(RequestParams.SYS_GROUP,"1");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        paramMap.put(RequestParams.BUSINESS_MONEY,"33.0");
        String[] recordId = {"01027231262167235"};
        for (int i =0;i<recordId.length;i++){
            paramMap.put(RequestParams.RECORD_ID,recordId[i]);
            Map<String,String> result=TradingServiceExecutor.doRpc(paramMap,false);
            System.out.println("recordId is "+recordId[i]+":"+result.get(ReplyRequestParams.STATUS));
        }*/
        //结合上面退款生成paramMap,从文件读入多条记录退款
        Map<String, String> map = new HashMap<String, String>();
        try {
            paramMap.put(RequestParams.GROUP_TYPE, GroupType.EPOS_PAY_REFUND);//先取消后退款GroupType.EPOS_PAY_CANCEL,EPOS_PAY_REFUND
            paramMap.put(RequestParams.SYS_GROUP,"1");
            paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
            FileReader file = new FileReader("E:\\work\\ucar\\test2.txt");
            BufferedReader reader = new BufferedReader(file);
            String input;
            int i =1;
            while((input=reader.readLine())!=null){
                String[] s = input.split("\t");
                paramMap.put(RequestParams.BUSINESS_MONEY,s[1]);
                paramMap.put(RequestParams.RECORD_ID, s[0]);
                Map<String,String> result=TradingServiceExecutor.doRpc(paramMap,false);
                System.out.println("第i个的结果是i=："+  i++   +"&&&&&&&&&&&&&&&&&&&====="+result.get(ReplyRequestParams.STATUS));
                if (result.get(ReplyRequestParams.STATUS).equals("0")) {
                    map.put(s[0],s[1]);
                }
                out.print(JSONArray.fromObject(result));
            }
            System.out.println("xx:"+ JSONArray.fromObject(map));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //查询卡信息
        /*paramMap.put(RequestParams.GROUP_TYPE,GroupType.QUERY_CARD_INFO);//GroupType.CREATE_NO_SMS
        paramMap.put(RequestParams.CARD_NO,"6226588842816062");//4041590000238132,,5201088010372655
        paramMap.put(RequestParams.SYS_GROUP, "1");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, false);
        //提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/
        //applepay
        /*paramMap.put(RequestParams.SYS_GROUP, "1");
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.APPLE_PAY);
        paramMap.put(RequestParams.BUSINESS_MONEY, "0.11");
        paramMap.put(RequestParams.BUSINESS_ID, "" + System.currentTimeMillis());
        paramMap.put(RequestParams.PAYER_ID, "110110");
        paramMap.put(RequestParams.BUSINESS_NAME, "applepaytest");
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap,true);
        System.out.println(result.get(ReplyRequestParams.STATUS));*/

        //查询支持列表
       /* paramMap.put(RequestParams.SYS_GROUP, "1");
        paramMap.put(RequestParams.CARD_TYPE,"1");//1,一键支付2,网银
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.QUERY_SUPPORT);
        paramMap.put(RequestParams.TARGET_GROUP,GroupType.QUERY_EPOS_BANK);
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        //提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/
        /*paramMap.put(RequestParams.GROUP_TYPE, GroupType.QUERY_SUPPORT);
        paramMap.put(RequestParams.TARGET_GROUP, GroupType.EPOS_PAY_SMS);
        paramMap.put(RequestParams.CARD_TYPE, "1");
        paramMap.put(RequestParams.IS_SCD, "true");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        //提取同步回复信息
        System.out.println(result.get(ReplyRequestParams.STATUS));*/
        //查询银行列表
       /* paramMap.put(RequestParams.GROUP_TYPE,GroupType.QUERY_EPOS_BANK);
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap,false);
        System.out.println(result.get(ReplyRequestParams.REPLY_INFO));//老接口租车用
        paramMap.put(RequestParams.GROUP_TYPE,GroupType.QUERY_EPOS_BANK);
        paramMap.put(RequestParams.TARGET_GROUP,GroupType.DRAWING);//GroupType.DRAWING
        paramMap.put(RequestParams.SYS_GROUP,"1");
        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        System.out.println(result.get(ReplyRequestParams.STATUS));
        System.out.println(result.get(ReplyRequestParams.REPLY_INFO));
        System.out.println(JSONArray.fromObject(result));*/
//        out.print(JSONArray.fromObject(result));

          //query
        /*String  url = "http://paytest02.10101111.com:9080/cartp";
//        String  url = "http://admindev.zuche.com:8081/cartp";
        RemoteClient client = RemoteClientFactory.getInstance(url, RemoteType.HESSIAN);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("payerId","5102125");
        map.put("stauts",1);
        map.put("cardType",1);
        Object jsonArray = client.executeToObject("cartp.creditcardbind.query",map);//"5395"4226459executeToObject
        System.out.println("&&&&&&&&&"+jsonArray.toString());*/
        //绑卡
        /*paramMap.put(RequestParams.GROUP_TYPE, GroupType.CREATE_NO_SMS);//EPOS_PAY_SMS,CREATE_NO_SMS
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        paramMap.put(RequestParams.DIRECT_CARD, "1");
        paramMap.put(RequestParams.PAYER_NAME, "张俊亮");
        paramMap.put(RequestParams.CARD_NO, "6214850220933086");//6225760019538417,6214850220933086
//        paramMap.put(RequestParams.CREDITCARD_EXPIREMONTH, "09");
//        paramMap.put(RequestParams.CREDITCARD_EXPIREYEAR, "20");
//        paramMap.put(RequestParams.CREDITCARD_CVV, "954");
        paramMap.put(RequestParams.PAYER_TEL, "15902283216");
        paramMap.put(RequestParams.IDENTITY_ID, "142601198909049112");
        paramMap.put(RequestParams.SOURCE_ID, "CMB");
        paramMap.put(RequestParams.BUSINESS_ID, "aaa123123123");
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.BUSINESS_NAME, "test");
        paramMap.put(RequestParams.PAYER_ID,"5125100");
        paramMap.put(RequestParams.CARD_TYPE, "2");
        paramMap.put(RequestParams.SYS_GROUP,"1");
//        paramMap.put(RequestParams.BIND_ID,"");
//        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);


//        System.out.println(result.get(ReplyRequestParams.STATUS));
//        System.out.println(result.get(ReplyRequestParams.REPLY_INFO));
//        System.out.println(JSONArray.fromObject(result));
//        if (result.get(ReplyRequestParams.STATUS).equals("1")) {
//            String Token = result.get(ReplyRequestParams.TOKEN);
//            String recordId = result.get(ReplyRequestParams.RECORD_ID);
            paramMap.put(RequestParams.TOKEN, "343003049");


            paramMap.put(RequestParams.VALID_CODE,"296876");
            paramMap.put(RequestParams.RECORD_ID, "0125773655423237");
            paramMap.put(RequestParams.GROUP_TYPE,GroupType.EPOS_PAY_SMS);
            paramMap.put(RequestParams.IS_VALIDATE,"true");
            Map<String,String> result1 = TradingServiceExecutor.doRpc(paramMap, true);
            System.out.println(result1.get(ReplyRequestParams.STATUS));
            System.out.println(result1.get(ReplyRequestParams.REPLY_INFO));
//        }*/


        //消费
        /*paramMap.put(RequestParams.GROUP_TYPE, GroupType.EPOS_PAY_SMS);
        paramMap.put(RequestParams.ASYNC_REPLY_URL,"http://10.101.32.132:8080/cartptest/testCartp/testMvcModeReturn.do_");
        paramMap.put(RequestParams.DIRECT_CARD, "1");
        paramMap.put(RequestParams.SYS_GROUP,"1");
        *//*paramMap.put(RequestParams.PAYER_NAME, "张俊亮");
        paramMap.put(RequestParams.CARD_NO, "6225760019538417");
        paramMap.put(RequestParams.CREDITCARD_EXPIREMONTH, "09");
        paramMap.put(RequestParams.CREDITCARD_EXPIREYEAR, "20");
        paramMap.put(RequestParams.CREDITCARD_CVV, "954");
        paramMap.put(RequestParams.PAYER_TEL, "15902283216");
        paramMap.put(RequestParams.IDENTITY_ID, "142601198909049112");
        paramMap.put(RequestParams.SOURCE_ID, "CMB");*//*

        paramMap.put(RequestParams.BIND_ID, "33630");
        paramMap.put(RequestParams.BUSINESS_ID, "aaa123123123");
        paramMap.put(RequestParams.BUSINESS_MONEY, "1");
        paramMap.put(RequestParams.BUSINESS_NAME, "test");

        Map<String,String> result = TradingServiceExecutor.doRpc(paramMap, true);
        System.out.println(result.get(ReplyRequestParams.STATUS));
        System.out.println(result.get(ReplyRequestParams.REPLY_INFO));
        System.out.println(JSONArray.fromObject(result));*/

        /*//银行卡支付
        // 1、请求标示
        paramMap.put(RequestParams.GROUP_TYPE, "eposPaySms");
        // 2、是否代驾项目
        paramMap.put(RequestParams.IS_SCD, "true");
        // 3、信用卡号
        paramMap.put(RequestParams.CARD_NO, "6214850220933086");
        // 7、银行预留电话号码
        paramMap.put(RequestParams.PAYER_TEL,"15902283216");
        // 8、身份证
        paramMap.put(RequestParams.IDENTITY_ID,"142601198909049112");
        paramMap.put(RequestParams.IDENTITY_TYPE, "1");
        // 9、信用卡姓名
        paramMap.put(RequestParams.PAYER_NAME,"张俊亮");
        // 10、银行简码
        paramMap.put(RequestParams.SOURCE_ID,"CMB");
        // 11、国家代码：保持默认
        paramMap.put(RequestParams.NATION_CODE,"");
        // 12、会员序号
        paramMap.put(RequestParams.PAYER_ID, "15993475");
        // 13、保持默认
        paramMap.put(RequestParams.DIRECT_CARD, "1");
        // 14、token
        paramMap.put(RequestParams.TOKEN, par.get("token").toString());
        // 15、recordId
        paramMap.put(RequestParams.RECORD_ID, par.get("recordId").toString());
        // 16、验证码
        paramMap.put(RequestParams.VALID_CODE, par.get("verifycode").toString());
        paramMap.put(RequestParams.ASYNC_REPLY_URL, projectAddress + "/bankcard/paycallback.do_");


        paramMap.put(RequestParams.CARD_TYPE,"2");//类型：储蓄卡
        paramMap.put(RequestParams.BUSINESS_MONEY, par.get("payAmount").toString()) ;
        paramMap.put(RequestParams.BUSINESS_NAME, BUSINESS_NAME) ;
        //调用接口
        Map<String,String> rpcResultMap = TradingServiceExecutor.doRpcBySeeting(paramMap);*/
//        out.print(JSONObject.fromObject(result));
    }

    @RequestMapping("/testRpcModeReturn.do_")
    @ResponseBody
    public String testRpcModeReturn(HttpServletRequest request,HttpServletResponse response){
        System.out.println("RPC模式,回调开始");
        String status = request.getParameter(ReplyRequestParams.STATUS);
        if(Status.SUCCESS.equals(status)){
            //开始回调处理
            //获得请求时发起的自定义参数
            String selfParam = request.getParameter(RequestParams.CUSTOMIZED_FLAG
                    +"selfParam");
            System.out.println(selfParam);

        }
        System.out.println("RPC模式,回调结束");
        //加入固定标记，通知交易平台回调成功
        response.addHeader("CAR_TP", Boolean.TRUE.toString());
        return null;
    }
    /*@RequestMapping("query.do_")
    public void query(HttpServletRequest request,HttpServletResponse response){
        //用来测试cartp远程服务，线上时连接可用
    String  url = "http://paytest02.10101111.com:9080/cartp";
//        String  url = "http://admindev.zuche.com:8081/cartp";
        RemoteClient client = RemoteClientFactory.getInstance(url, RemoteType.HESSIAN);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("payerId","5102125");
        map.put("stauts",1);
        map.put("cardType",1);
        Object jsonArray = client.executeToObject("cartp.creditcardbind.query",map);//"5395"4226459executeToObject
        System.out.println("&&&&&&&&&"+jsonArray.toString());
    }*/




}
